//
//  UIWebView+MZUserAgent.m
//  MZToolkitDemo
//
//  Created by 沈家林 on 2017/5/12.
//  Copyright © 2017年 Chris Yang. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "UIWebView+MZUserAgent.h"

@implementation UIWebView (MZUserAgent)

+(void)mzConfigUserAgentWithDeviceIdentifier:(NSString *)deviceIdentifier
                           withNetworkType:(NSString *)networkType {
    
    NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    NSString *phoneBrand = @"apple";
    UIWebView *tmpWebView = [[UIWebView alloc] init];
    NSString* userAgent = [tmpWebView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
    NSString *ua = [NSString stringWithFormat:@"%@ | "
                    "{\"deviceIdentifier\":\"%@\","
                    "\"appVersion\":\"%@\","
                    "\"phoneBrand\":\"%@\","
                    "\"networkType\":\"%@\"}",
                    userAgent,
                    deviceIdentifier,
                    appVersion,
                    phoneBrand,
                    networkType];
    [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent" : ua, @"User-Agent" : ua}];
}

@end
